package com.financing.wap.service.impl;


import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.financing.wap.common.CommonConstantUtil.CommonStatus;
import com.financing.wap.dao.IDstUserTagListDao;
import com.financing.wap.dto.DstUserClientTagInfoDTO;
import com.financing.wap.dto.DstUserTagListDTO;
import com.financing.wap.service.IDstUserClientTagInfoService;
import com.financing.wap.service.IDstUserTagListService;
import com.financing.wap.util.DTOdateSetUtil;
/**
 * dst_user_tag_list  
 * 用户标签表 业务访问层实现类
 * @author huqiyun
 *
 */
 
@Service("dstUserTagListService")
@Transactional(rollbackFor=Exception.class)
public class DstUserTagListServiceImpl implements IDstUserTagListService{

	@Autowired
	private IDstUserTagListDao dstUserTagListDao;
	
	@Autowired
	private IDstUserClientTagInfoService dstUserClientTagInfoService;

	@Override
	public int insert(DstUserTagListDTO dstUserTagList) throws SQLException {
		DTOdateSetUtil.setCreateInfo(dstUserTagList);
		DTOdateSetUtil.setUpdateInfo(dstUserTagList);
		dstUserTagList.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
		return dstUserTagListDao.insert(dstUserTagList);
	}

	@Override
	public void update(DstUserTagListDTO dstUserTagList) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(dstUserTagList);
		dstUserTagListDao.update(dstUserTagList);
	}

	@Override
	public void updateForce(DstUserTagListDTO dstUserTagList) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(dstUserTagList);
		dstUserTagListDao.updateForce(dstUserTagList);
	}

	@Override
	public void delete(Integer id) throws SQLException {
		dstUserTagListDao.delete(id);
	}
	
	@Override
	public List<DstUserTagListDTO> queryList(DstUserTagListDTO dstUserTagList) throws SQLException {
		return dstUserTagListDao.queryList(dstUserTagList);
	}

	@Override
	public int queryListCount(DstUserTagListDTO dstUserTagList) throws SQLException {
		return dstUserTagListDao.queryListCount(dstUserTagList);
	}

	@Override
	public DstUserTagListDTO loadById(Integer id) throws SQLException {
		return dstUserTagListDao.loadById(id);
	}
 	@Override
	public DstUserTagListDTO queryListGetFirst(DstUserTagListDTO dstUserTagList)
			throws SQLException {
		 List<DstUserTagListDTO> queryList = queryList(dstUserTagList);
		 if(queryList==null||queryList.size()==0){
			 return new DstUserTagListDTO();
		 }
		 return queryList.get(0);
	}

	@Override
	public void deleteTag(String userOpenid,Integer tagId) throws Exception {
		
		//删除标签
		DstUserTagListDTO dto=new DstUserTagListDTO();
		dto.setId(tagId);
		dto.setDeleteTag(CommonStatus.DELETE_TAG_DELETE);
		update(dto);
		
		//删除用户
		DstUserClientTagInfoDTO dstUserClientTagInfo=new DstUserClientTagInfoDTO();
		dstUserClientTagInfo.setUserOpenid(userOpenid);
		dstUserClientTagInfo.setClientTagId(tagId);
		dstUserClientTagInfo.setDeleteTag(CommonStatus.DELETE_TAG_DELETE);
		dstUserClientTagInfoService.update(dstUserClientTagInfo);
		
	}

	@Override
	public synchronized void addTagToClient(String userOpenid, String tagName,
			String clientOpenid) throws Exception {
		//判断
		DstUserTagListDTO dstUserTagList=new DstUserTagListDTO();
		dstUserTagList.setUserOpenid(userOpenid);
		dstUserTagList.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
		dstUserTagList.setUserTagName(tagName);
		DstUserTagListDTO dto = queryListGetFirst(dstUserTagList);
		int tagId=0;
		if(dto.getId()==null){
			tagId=insert(dstUserTagList);
		}else{
			tagId=dto.getId();
		}
		DstUserClientTagInfoDTO clientTag=new DstUserClientTagInfoDTO();
		clientTag.setUserOpenid(userOpenid);
		clientTag.setUserClientOpenid(clientOpenid);
		clientTag.setClientTagId(tagId);
		clientTag.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
		DstUserClientTagInfoDTO d = dstUserClientTagInfoService.queryListGetFirst(clientTag);
		if(d.getId()==null){
			clientTag.setClientTagName(tagName);
			dstUserClientTagInfoService.insert(clientTag);
		}
	}
}
